//
//  LoanMainTopView.m
//
//
//  Created by gynjobs on 2017/8/14.
//  Copyright © 2017年 gynjobs. All rights reserved.
//

#import "LoanMainTopView.h"
#import "HomeMainAction.h"


@interface LoanMainTopView()<UIScrollViewDelegate>
@property (nonatomic,weak)UILabel *  selectLable;
@property (nonatomic,weak)UILabel *  otherLable;
@property (nonatomic,weak)UILabel *  loadLable;
@property (nonatomic,weak)UILabel *  craditCardLable;
@property (nonatomic,weak)UIView * middleCover;
@property (nonatomic,assign)NSInteger previousIndex;
@property (nonatomic,weak)UIScrollView * scrollView;

@property (nonatomic,assign)CGFloat  centerDistance;
@end

@implementation LoanMainTopView


+ (instancetype)mainTopView{
    LoanMainTopView *topView = [[LoanMainTopView alloc] init];
    return topView;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.previousIndex = 0;
        [self setupSubViews];
        //        [self testView];
    }
    return self;
}

- (void)setupSubViews{
    UIView * middleCover = [[UIView alloc] init];
    middleCover.layer.cornerRadius = lableH*0.5;
    middleCover.layer.masksToBounds = YES;
    middleCover.layer.borderWidth = 0.5f;
    middleCover.layer.borderColor = [UIColor colorWithRed:66/255.0 green:73/255.0 blue:99/255.0 alpha:0.2].CGColor;
    middleCover.backgroundColor = [UIColor whiteColor];
    [self addSubview:middleCover];
    self.middleCover = middleCover;
    [middleCover mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.height.mas_equalTo(lableH);
        make.width.mas_equalTo(CoverViewWidth);
    }];
    
    
    UIScrollView * scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor clearColor];
    
    scrollView.delegate =self;
    scrollView.contentSize = CGSizeMake(CoverViewWidth*2+10, lableH);
    scrollView.bounces =NO;
    [self addSubview:scrollView];
    self.scrollView =scrollView;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(self);
    }];
    
    
    UILabel * loadLable = [self createLableWithTextColor:SelectLableTextColor fontSize:SelectLableFontSize title:[[HomeMainAction getMainTopViewData] firstObject]];
    loadLable.tag =100;
    [scrollView addSubview:loadLable];
    self.loadLable = loadLable;
    self.selectLable = loadLable;
    
    
    UILabel * craditCardLable = [self createLableWithTextColor:normalLableTextColor fontSize:normalLableFontSize title:[[HomeMainAction getMainTopViewData] lastObject]];
    craditCardLable.tag =101;
    [scrollView addSubview:craditCardLable];
    self.craditCardLable = craditCardLable;
    self.otherLable = craditCardLable;
    
    
    [loadLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.scrollView.mas_centerX);
        make.centerY.mas_equalTo(self.scrollView.mas_centerY);
        make.height.mas_equalTo(lableH);
        make.width.mas_equalTo(CoverViewWidth);
    }];
    CGFloat middleMargin = 10;
    //中心点距离
    self.centerDistance = (CoverViewWidth+(CoverViewWidth+10))*0.5+middleMargin;
    [craditCardLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(scrollView.mas_centerY);
        make.height.mas_equalTo(lableH);
        make.width.mas_equalTo(CoverViewWidth+10);
        make.left.mas_equalTo(loadLable.mas_right).offset(middleMargin);
    }];
}

#pragma mark ---处理滑动问题---

- (void)changeScrollViewWithRation:(CGFloat)offsetRation withAnimation:(BOOL)animation{
    if(offsetRation<0)return;
    CGFloat rightFont = 15+offsetRation*3;
    CGFloat leftFont = 15 + (1 - offsetRation)*3;
    CGFloat rOffset = 131,gOffset = 129,bOffset = 127;
    
    [self.middleCover mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(CoverViewWidth + 10*offsetRation);
    }];
    
    
    //    NSLog(@"leftRGB:%lf,%lf,%lf",(165 - (1 - offsetRation)*rOffset), (167 - (1 - offsetRation)*gOffset), (178 - (1 -offsetRation)*bOffset));
    //
    //     NSLog(@"RightRGB:%lf,%lf,%lf",(165 - offsetRation*rOffset), (167 - offsetRation*gOffset), (178 - offsetRation*bOffset));
    
    self.loadLable.textColor = RGBColor((165 - (1 - offsetRation)*rOffset), (167 - (1 - offsetRation)*gOffset), (178 - (1 -offsetRation)*bOffset));
    
    self.craditCardLable.textColor = RGBColor((165 - offsetRation*rOffset), (167 - offsetRation*gOffset), (178 - offsetRation*bOffset));
    self.loadLable.font = [UIFont systemFontOfSize:leftFont];
    self.craditCardLable.font = [UIFont systemFontOfSize:rightFont];
    
    CGFloat scrollW = self.centerDistance;
    [self.scrollView setContentOffset:CGPointMake(scrollW*offsetRation, 0) animated:animation];
}
- (void)changeSelectLableWithIndex:(NSInteger)index{
    if(self.previousIndex == index) return;
    self.previousIndex = index;
    self.otherLable = self.selectLable;
    if(index == 1){
        self.selectLable = self.craditCardLable;
    }else{
        self.selectLable = self.loadLable;
    }
    CGFloat offetW = 1.5;
    if(index>0){
        offetW += CoverViewWidth+lableMargin;
    }
    
    [UIView animateWithDuration:AnimationDuration animations:^{
        self.selectLable.font = [UIFont systemFontOfSize:SelectLableFontSize];
        self.selectLable.textColor = SelectLableTextColor;
        self.otherLable.font = [UIFont systemFontOfSize:normalLableFontSize];
        self.otherLable.textColor = normalLableTextColor;
        
        [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.middleCover.mas_left).offset(-offetW);
        }];
        
        [self.middleCover mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(index>0?(CoverViewWidth+10):CoverViewWidth);
        }];
        
    }];
    
    
    
    
    
}


- (UILabel *)createLableWithTextColor:(UIColor *)textColor fontSize:(CGFloat)fontSize title:(NSString *)title{
    UILabel * lable = [[UILabel alloc] init];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.textColor = textColor;
    lable.font = [UIFont systemFontOfSize:fontSize];
    lable.text = title;
    return lable;
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
